package com.coffee.test;

import com.coffee.netty.connection.CoffeeConnectionFactory;
import com.coffee.netty.connection.Connection;

/**
 * @author songkui
 * @since 2024/8/5 17:52
 */
public class HjClient {

    private final CoffeeConnectionFactory connectionFactory;
    private static final HjClient client = new HjClient();
    private HjClient(){
        connectionFactory = new CoffeeConnectionFactory();
        connectionFactory.registerCommandHandler(new HjCommandHandler() );
        connectionFactory.registerRpcCodec(new HjCoder());
        connectionFactory.registerHeartbeatHandler(new HeartHandler());
        connectionFactory.setHeartSecond(60L);
        connectionFactory.init();
    }
    public static HjClient getInstance(){
        return client;
    }

    public Connection connection(Long userId, String ip, int port, int serverId){
        Connection connection = new Connection(userId, ip, port, 1);
        try {
            connectionFactory.connect(connection);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    public Connection connection(Long userId, String ip, int port, int serverId, int reconnectTime){
        Connection connection = new Connection(userId, ip, port, 1)
                .ofReconnect(true, reconnectTime);
        try {
            connectionFactory.connect(connection);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return connection;
    }


}
